package com.example.routeplansystem.algorithm.imp;

import com.alibaba.fastjson.JSON;
import com.example.routeplansystem.algorithm.Algorithm;
import com.example.routeplansystem.entity.po.FinalSolution;
import com.example.routeplansystem.entity.po.NodePojo;
import com.example.routeplansystem.entity.po.Solution;
import com.example.routeplansystem.entity.po.VehicleMessage;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author lucong
 * @Date 2022/5/13
 * @Description 按存储顺序平均分
 */
@Component
public class SequenceAlgorithm implements Algorithm {

    @Override
    public List<Solution> getSolution(List<NodePojo> nodes,
                                      List<NodePojo> centerNodes,
                                      List<VehicleMessage> vehicles,
                                      FinalSolution finalSolution) {
        int finalSolutionId = finalSolution.getFinalSolutionId();
        NodePojo center = centerNodes.get(0);
        int routeLen = nodes.size() / vehicles.size() + (nodes.size() % vehicles.size() == 0 ? 0 : 1); // 除法向上取整
        List<List<NodePojo>> partition = ListUtils.partition(nodes, routeLen + 1); // 将节点平均分开
        List<Solution> solutions = new ArrayList<>();
        for (int i = 0; i < vehicles.size(); i++) {
            Solution solution = new Solution();
            solution.setVehicleId(vehicles.get(i).getVehicleId());
            List<NodePojo> routeList = partition.get(i);
            routeList.add(0, center);
            routeList.add(center);
            String route = JSON.toJSONString(routeList);
            solution.setRoute(route);
            solution.setFinalSolutionId(finalSolutionId);
            solutions.add(solution);
        }
        return solutions;
    }

    @Override
    public void checkMatrix(Integer questionId, Integer finalSolutonId) {

    }
}
